package cn.software.service.impl;

import cn.software.entity.Paper;
import cn.software.entity.param.PaperParam;
import cn.software.mapper.PaperMapper;
import cn.software.service.PaperService;
import cn.software.service.TopicPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PaperServiceImpl implements PaperService {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private TopicPaperService topicPaperService;
    /**
     * 上传试卷
     * @param paperParam 试卷信息
     * @return 是否成功
     */
    @Override
    public boolean uploadPaper(PaperParam paperParam) {
        //转化对象（只有名字、总分和科目id）
        Paper paper = transformPaperParamToPaper(paperParam);
        int paperInsert = paperMapper.insert(paper);

        //存储试卷题目列表
        boolean topicListInsert = topicPaperService.saveTopicListOfPaper(paper.getId(), paperParam);
        return paperInsert > 0 && topicListInsert;
    }

    /**
     * paperParam转化为paper
     * @param paperParam 传入的paperparam
     * @return 返回paper
     */
    private Paper transformPaperParamToPaper(PaperParam paperParam) {
        Paper paper = new Paper();
        paper.setName(paperParam.getPaperName());
        paper.setTotalScore(paperParam.getPaperScore());
        paper.setSubjectId(paperParam.getPaperSubject());
        return paper;
    }
}
